<?php

defined('MYLITS') ? '' : exit('No direct script access allowed');

/**
 * @version     1.0.0
 * @package     mylits engine
 * @license     GNU General Public License version 3
 * @author      Arfan Fudyartanto D N <arfan@mylits.com> - http://engine.mylits.com
 */
class usersModel extends Mylits_Model {

    function __construct() {
        parent::__construct();
        $this->setConnectionNumber(0);
        $this->setSqlFile('users');
    }

    public function getUserList() {
        $query = $this->openQuery('get_user_list');
        return $this->runQuery($query);
    }

    public function addUser($username, $email, $name, $password) {
        $query = $this->openQuery('add_user', array($username, $email, $name, $password));
        return $this->runQuery($query);
    }

    public function dellUser($id) {
        $query = $this->openQuery('dell_user', array($id));
        return $this->runQuery($query);
    }

    public function getGroupList() {
        $query = $this->openQuery('get_group_list');
        return $this->runQuery($query);
    }

    public function isUsernameExist($username) {
        $query = $this->openQuery('is_username_exist', array($username));
        $result = $this->runQuery($query);
        if (!empty($result) && isset($result[0]['count']) && $result[0]['count'] >= 1) {
            return true;
        } else {
            return false;
        }
    }

    public function addGroupMap($userId, $groupId) {
        $query = $this->openQuery('add_group_map', array($userId, $groupId));
        return $this->runQuery($query);
    }

    public function getUserDetail($userId) {
        $query = $this->openQuery('get_user_detail', array($userId));
        $result = $this->runQuery($query);
        if (!empty($result)) {
            return $result[0];
        }
    }

    public function getUserGroupList($userId) {
        $query = $this->openQuery('get_user_group_list', array($userId));
        $result = $this->runQuery($query);
        if (!empty($result)) {
            return $result[0]['group_id'];
        }
    }

    public function updateUser($username, $email, $name, $userId) {
        $query = $this->openQuery('update_user', array($username, $email, $name, $userId));
        return $this->runQuery($query);
    }
    
    public function deleteGroupMap($userId){
        $query = $this->openQuery('delete_group_map', array($userId));
        return $this->runQuery($query);
    }
    
    public function updatePassword($newPassword, $userId){
        $query = $this->openQuery('update_password', array($newPassword, $userId));
        return $this->runQuery($query);
    }

}

?>
